<?php

$db = mysql_connect("localhost", "root", "");
mysql_select_db("cms");
mysql_set_charset('utf8');

include 'functions.php';
include 'class/users.php';
include 'class/auth.php';
include 'class/settings.php';
include 'class/article.php';
include 'class/category.php';
include 'class/page.php';
include 'class/web.php';

session_start();

if (!isset($_SESSION['auth'])) {
	$_SESSION['auth'] = new Auth();		
}



if (isset($_POST["prihlasit"])) {
	$_SESSION['auth']->Login($_POST["nick"], $_POST["password"]);
	$_SESSION["control"] = serialize($_SESSION["auth"]);
}

if (isset($_POST["pridat"])) {
	Users::newUser($_POST["nick"], $_POST["password"], $_POST["jmeno"], $_POST["prijmeni"], $_POST["email"]);
}

if (isset($_POST["odebrat"])) {
	Users::deleteUser($_POST["nick"]);
}
if (isset($_POST["heslo"])) {
	Users::changePassword($_POST["old"], $_POST["new"],$_POST["control"]);
}

if (isset($_POST["zmenit"])) {
	Users::changeData($_SESSION["auth"]->getNick(), $_POST["jmeno"], $_POST["prijmeni"], $_POST["email"], $_POST["password"]);
}

if(isset($_POST["nastav"])){
	Settings::setAll($_POST["title"], $_POST["keywords"], $_POST["description"]);
}

if(isset($_POST["pridat_clanek"])){
	Article::newArticle($_POST["nazev"], $_POST["anotace"], $_POST["obsah"], $_POST["autor"], $_POST["kategorie"], $_POST["publikovat"]);
}
if(isset($_GET["smazat_clanek"])){
	Article::deleteArticle($_GET["id"]);
	header("Location:index.php?clanky");
}
if(isset($_POST["upravit_clanek"])){
	Article::editArticle($_POST["id"], $_POST["nazev"], $_POST["anotace"], $_POST["obsah"], $_POST["kategorie"]);
	header("Location:index.php?clanky");
}

if(isset($_POST["upravit_stranku"])){
	Page::editPage($_POST["id"], $_POST["nazev"], $_POST["obsah"]);
	header("Location:index.php?stranky");
}

if (isset($_POST["pridat_kategorii"])) {
	Category::newCategory($_POST["nazev"], $_POST["parent"]);
}

if (isset($_POST["pridat_stranku"])) {
	Page::newPage($_POST["nazev"], $_POST["obsah"]);
}

if(isset($_GET["smazat_stranku"])){
	Page::deletePage($_GET["id"]);
	header("Location:index.php?stranky");
}
if(isset($_GET["odhlas"])){	
	$_SESSION["auth"]->Logout();
	session_unregister("control");
	header("Location:index.php");
}

?>